﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;


namespace Administrare
{
    public partial class NavigareFrm : FrmRezumat
    {
        private FrmMainMdi mainFrm = null;
        private ToolStripTextBox urlTB = null;
        private HtmlDocument document = null;
        public NavigareFrm(ModuleClass mc):base(mc)
        {
            InitializeComponent();
        }

        private void NavigareFrm_Load(object sender, EventArgs e)
        {
            urlTB = AddTextBox("newTextBox", ToolStripItemAlignment.Left, 300);
            AddButton("Go", global::ERPStudio.Properties.Resources.refresh_48, Navigare, ToolStripItemAlignment.Right, false);
            urlTB.KeyDown += new KeyEventHandler(urlTB_KeyDown);
            webBrowser1.CanGoBackChanged += new EventHandler(webBrowser1_CanGoBackChanged);
            mainFrm = MdiParent as FrmMainMdi;
            Navigate("www.google.com");
        }

        void webBrowser1_CanGoBackChanged(object sender, EventArgs e)
        {
            mainFrm.AnteriorTSB.Enabled = webBrowser1.CanGoBack;
        }

        void urlTB_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Navigate(urlTB.Text);
            }

        }

        private void Navigare(object sender, EventArgs e)
        {


            if (urlTB != null)
                Navigate(urlTB.Text);

            //PostRequest("http://apt1.mfinante.ro/site/contribuabili/link.jsp?body=/cod.do", "cod", urlTB.Text);
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            
            
            //MessageBox.Show(e.Url.ToString());
        }
        private void Navigate(String address)
        {
            if (String.IsNullOrEmpty(address)) return;
            if (address.Equals("about:blank")) return;
            if (!address.StartsWith("http://") &&
                !address.StartsWith("https://"))
            {
                address = "http://" + address;
            }
            try
            {
                webBrowser1.Navigate(new Uri(address));
            }
            catch (System.UriFormatException)
            {
                return;
            }
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            document = this.webBrowser1.Document;
            //propertyGrid1.SelectedObject = document;
            if (urlTB != null)
                urlTB.Text = webBrowser1.Url.ToString();
           
        }
        override public void Urmator(object sender, EventArgs args)
        {
            if (urlTB != null)
                Navigate(urlTB.Text);

        }
        override public void Anterior(object sender, EventArgs args)
        {
            webBrowser1.GoBack();
        }
        
        private void IEPostStringRequest(String URL, String FormData)
        {
            //Send the form data To URL As POST request
            
            //ReDim bFormData(Len(FormData) - 1)
            Encoding a = System.Text.Encoding.UTF8;
            Byte[] bFormData = a.GetBytes(FormData);
            //bFormData = StrConv(FormData, vbFromUnicode)
            
            webBrowser1.Navigate(URL, "_self", bFormData, "Content-Type: application/x-www-form-urlencoded\r\n");
            
            Application.DoEvents();
        }

        private void  PostRequest(String URL,String Names, String Values)
        {
            String FormData = String.Empty;
            String Name=String.Empty, Value=String.Empty;

            //Enumerate form names And it's values
            //and built string representaion of the form data
            
            Name = URLEncode(Names);
            Value = URLEncode(Values);
            if(FormData != String.Empty)
                FormData = FormData + "&";
            FormData = FormData + Name + "=" + Value;

            //Post the data To the destination URL

            IEPostStringRequest(URL, FormData);
        }

        private String URLEncode(String Data)
        {
            Char c;
            String Out=String.Empty;

            for(Int16 I = 0;I < Data.Length;I++)
            {
                //c = Asc(Mid(Data, I, 1))
                c= Data[I];
                if(c == 32)
                    Out = Out + "+";
                else 
                    if(c < 48) 
                        Out = Out + "%" + String.Format("{0:X}",c); 
                    else
                        Out = Out + c.ToString();
            }
            return Out;
        }






    }
}
